package com.sparrow.manage.article;

import com.sparrow.config.EnvironmentVariables;
import com.sparrow.manage.common.controller.StandardController;
import com.sparrow.manage.common.entity.Attachment;
import com.sparrow.manage.common.entity.PageInfo;
import com.sparrow.manage.common.entity.result.Result;
import com.sparrow.manage.common.service.impl.AttachmentService;
import com.sparrow.manage.common.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(EnvironmentVariables.MANAGE_ACCESS_PREFIX + "/article")
public class ArticleController extends StandardController<Article> {

    private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

    private static final List<Article.ArticleType> articleTypes = Arrays.asList(Article.ArticleType.values());

    @Autowired
    private ArticleService articleService;

    @Autowired
    private AttachmentService attachmentService;

    @RequestMapping("/index")
    public String index(){
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "article/index";
    }

    @RequestMapping("editor")
    public String editor(String articleId, Model model){
        model.addAttribute("content", "");
        if(StringUtils.isNotBlank(articleId)){
            Article article = articleService.queryById(articleId);
            if(article != null){
                model.addAttribute("content", article.getContent());
            }
        }
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "article/editor";
    }

    @RequestMapping("/addPage")
    public String addPage(Model model){
        model.addAttribute("articleTypes", articleTypes);
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "article/add";
    }

    @RequestMapping("/editPage")
    public String editPage(String id, Model model){
        Article article = articleService.queryById(id);
        Attachment attachment = new Attachment();
        if(article != null){
            List<Attachment> atts = attachmentService.queryByRelateId(article.getId());
            if(atts != null && !atts.isEmpty()){
                attachment = atts.get(0);
            }
        }
        model.addAttribute("attachment", attachment);
        model.addAttribute("article", article);
        model.addAttribute("articleTypes", articleTypes);
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "article/edit";
    }

    @RequestMapping("/queryPage")
    @ResponseBody
    public PageInfo<Article> queryPage(Article article, Integer page, Integer limit){
        return articleService.queryPage(article, limit, page - 1, "createTime", "desc");
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(Article article){
        return this.addEntity(article);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(String id){
        return this.removeEntity(id);
    }

    @ResponseBody
    @RequestMapping("/update")
    public Result update(Article article){
        return this.updateEntity(article);
    }

    @ResponseBody
    @RequestMapping("/updateCarousel")
    public Result updateCarousel(String id, Integer carousel){
        Result result = new Result();
        try{
            this.articleService.updateCarousel(id, carousel);
            result.setSuccess(true);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return result;
    }

    @Override
    protected BaseService getService() {
        return articleService;
    }

    @Override
    protected Result argumentsValidate(Article article) {
        return null;
    }

    @RequestMapping("types")
    @ResponseBody
    public Map<String, String> articleTypes(){
        Map<String, String> typeMap = new HashMap<String, String>();
        for(Article.ArticleType type : ArticleController.articleTypes){
            typeMap.put(type.getCode(), type.getName());
        }
        return typeMap;
    }
}
